<?php

class TagsAction extends PublicAction {

    protected  $db,$fields;

    public function _initialize()
    {

        parent::_initialize();

        defined('THEME_PATH') or define('THEME_PATH', TMPL_PATH.GROUP_NAME.'/'.C('DEFAULT_THEME').'/');

        $this->db = D(MODULE_NAME);

        $fields = F($this->moduleid.'_Field');
        foreach($fields as $key => $res){
          $res['setup'] = string2array($res['setup']);
          $this->fields[$key] = $res;
        }

        $this->assign('fields',$this->fields);
        $this->assign('module',$this->module);
    }

    function index()
    {

        $list = M('tags')->select();

        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 更新
     *
     */
    function edit()
    {

        $model = M('Tags');
        $pk = ucfirst($model->getPk());
        $id = $_REQUEST[$model->getPk()];

        if(empty($id))
            $this->error(L('do_empty'));

        $do = 'getBy'.$pk;
        $vo = $model->$do( $id );

        if($vo['setup'])
          $vo['setup'] = string2array($vo['setup']);

        $this->assign('vo', $vo);

        $this->display();
    }

    function update()
    {

        $name = 'Tags';
        $model = D( $name );

        if (false === $model->create()) {
          $this->error($model->getError ());
        }

        if (false !== $model->save ()) {

            if(in_array(ucfirst($name),$this->cache_model))
                savecache($name);

            if($_POST['isajax'])
                $this->assign('dialog','1');

            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(MODULE_NAME.'/index');

            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('edit_ok'));
        } else {
            $this->success (L('edit_error').': '.$model->getDbError());
        }
    }

}